package com.example.meitao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.meitao.R;
import com.example.meitao.bean.Discount;
import com.example.meitao.config.NetConfig;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 星星 on 2016/8/30.
 */
public class DiscountAdapter extends BaseAdapter{
    List<Discount.Databean.Resultsbean> data;
    private Context context;

    public DiscountAdapter(List<Discount.Databean.Resultsbean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final MyViewHolder holder;
        if(view==null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_discount,null);
            holder = new MyViewHolder(view);
            view.setTag(holder);
        }else{
            holder = (MyViewHolder) view.getTag();
        }
        Discount.Databean.Resultsbean resultsbean = data.get(i);
        holder.t1.setText(resultsbean.getOrigin_name());
        holder.t2.setText(resultsbean.getHighlight());
        holder.t3.setText(resultsbean.getEname());

        holder.t5.setText(resultsbean.getEditor().getNick()+"推荐");
        holder.t6.setText(resultsbean.getComments()+"");
        holder.t7.setText(resultsbean.getLikes()+"");
        Picasso.with(context).load(resultsbean.getEntitypicv2().get(0).getFilename()).into(holder.i1);
        if(resultsbean.getPrice()!=0) {
            holder.t8.setText(resultsbean.getCurrency_symbol());
            holder.t4.setText(resultsbean.getPrice()+"");
        }else{
            holder.t4.setText(resultsbean.getHighlight());
            holder.t8.setText("");
        }
        Picasso.with(context).load(NetConfig.IMAGE_PATH+resultsbean.getEditor().getAvatar()).into(holder.i3);

        return view;
    }
    public static class MyViewHolder{
        private TextView t1,t2,t3,t4,t5,t6,t7,t8;
        private ImageView i1,i3;

        public MyViewHolder(View view) {
            t1 = (TextView) view.findViewById(R.id.tv_item_discount_difang);
            t2 = (TextView) view.findViewById(R.id.tv_item_discount_tejia);
            t3 = (TextView) view.findViewById(R.id.tv_item_discount_jianjie);
            t4 = (TextView) view.findViewById(R.id.tv_item_discount_jiage);
            t5 = (TextView) view.findViewById(R.id.tv_item_discount_zuozhe);
            t6 = (TextView) view.findViewById(R.id.tv_item_discount_ping);
            t7 = (TextView) view.findViewById(R.id.tv_item_discount_zan);
            i1 = (ImageView) view.findViewById(R.id.iv_item_discount_show);
            t8 = (TextView) view.findViewById(R.id.tv_item_discount_money);
            i3 = (ImageView) view.findViewById(R.id.iv_item_discount_head);

        }
    }
}
